package com.wbao.dianniu.im;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.wbao.dianniu.adapter.FriendSearchAdapter;
import com.wbao.dianniu.data.AccountData;
import com.wbao.dianniu.listener.IFriendRecommendListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FriendRecommendManager;
import com.wbao.dianniu.manager.ShareSDKManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.ui.InviteFriendActivity;
import com.wbao.dianniu.ui.PerfectInfoActivity;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

public class AddFriendActivity extends BaseActivity implements IFriendRecommendListener {

	private EditText mEditText;
	private ListView mListView;
	private FriendSearchAdapter adapter;
	private List<AccountData> mList;
	private LinearLayout recommendLayout;
	private ImageView addBook;
	private ImageView addWeichart;
	private ImageView addQQ;
	private FriendRecommendManager recomManager;
	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_add_friends,
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		setTitleName("添加好友");
		init();
		initManager();
	}

	private void initManager(){
		recomManager = new FriendRecommendManager(this);
		recomManager.addFriendRecomListener(this);
		recomManager.friendRecommendList(GlobalContext.accountId, 0, 100);
	}
	
	
	private void init() {
		mEditText = (EditText) findViewById(R.id.message_search_edit);
		mListView = (ListView) findViewById(R.id.message_search_listview);
		mList = new ArrayList<>();
		adapter = new FriendSearchAdapter(this);
		adapter.setData(mList);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//这里要利用adapter.getItem(position)来获取当前position所对应的对象
				Integer accountId = ((AccountData)adapter.getItem(position)).getId();
				Utils.intoHeadInfoActivity(accountId,AddFriendActivity. this);
			}
		});
		
		recommendLayout = (LinearLayout) findViewById(R.id.recommend_layout);
		addBook = (ImageView) findViewById(R.id.add_book_to_friend);
		addWeichart = (ImageView) findViewById(R.id.add_weichart_to_friend);
		addQQ = (ImageView) findViewById(R.id.add_qq_to_friend);
		addBook.setOnClickListener(this);
		addWeichart.setOnClickListener(this);
		addQQ.setOnClickListener(this);
		mEditText.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.message_search_edit:
			//点击输入框，跳转到搜索界面
			Intent searchIntent = new Intent();
			searchIntent.setClass(AddFriendActivity.this, FriendSearchActivity.class);
			startActivity(searchIntent);
			break;
		case R.id.add_book_to_friend:
//			if(!GlobalContext.isPerfectData){
//				showPerfectAuth("请先完善资料");
//				return;
//			}
			Intent inviteIntent = new Intent();
			inviteIntent.setClass(this, InviteFriendActivity.class);
			startActivity(inviteIntent);
			break;
		case R.id.add_weichart_to_friend:
			if(!GlobalContext.isPerfectData){
				showPerfectAuth("请先完善资料");
				return;
			}
			// 调用分享内容
			String wc_imageUrl = GlobalContext.cdndownUrl +  GlobalContext.headPic;
			String wc_content = "我正在使用电纽app，趣味多多，大家赶快加入吧：https://www.dianniu.me/down/index.html";
			ShareSDKManager.getInstance().showShareToWechat(this,wc_content,wc_imageUrl,GlobalContext.accountId);
			break;
		case R.id.add_qq_to_friend:
			if(!GlobalContext.isPerfectData){
				showPerfectAuth("请先完善资料");
				return;
			}
			// 调用分享内容
			String qq_imageUrl = GlobalContext.cdndownUrl + GlobalContext.headPic;
			String qq_content = "我正在使用电纽app，趣味多多，大家赶快加入吧：https://www.dianniu.me/down/index.html";
			ShareSDKManager.getInstance().showShareToQQ(this,qq_content,qq_imageUrl,GlobalContext.accountId);
			break;
		default:
			break;
		}
	}
	
	/**
	 * 显示完善资料弹出框
	 */
	private void showPerfectAuth(String msg) {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view
				.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				// 跳转到完善资料界面
				Intent intent = new Intent();
				intent.setClass(AddFriendActivity.this, PerfectInfoActivity.class);
				startActivity(intent);
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}

	@Override
	public void onFriendRecomFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFriendRecomSuccess(List<AccountData> objdata) {
		// TODO Auto-generated method stub
		if(null != objdata){
			recommendLayout.setVisibility(View.VISIBLE);
			adapter.cleardata();
			mList.clear();
			mList = objdata;
			adapter.setData(mList);
			adapter.notifyDataSetChanged();
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (null != dialog && dialog.isShowing()) {
			dialog.dismiss();
		}
	}

}
